package com.robert.demo.controller;

import com.robert.common.commonutil.bean.vo.ResultData;
import com.robert.demo.common.utils.AesUtil;
import com.robert.demo.entity.CaseInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * CaseController:
 *
 * @author zhaoshengtao
 * @date 2024/03/21
 */
@RestController
@Slf4j
@RequestMapping("/case/")
public class CaseController {

    // 模拟查询的结果
    private static final List<CaseInfo> CASE_INFO_LIST = new ArrayList<>();
    static {
        CASE_INFO_LIST.add(CaseInfo.builder().id(1001L).name("张三").build());
        CASE_INFO_LIST.add(CaseInfo.builder().id(1002L).name("李四").build());
        CASE_INFO_LIST.add(CaseInfo.builder().id(1003L).name("王五").build());
    }
    /**
     * 模拟单个查询
     */
    @GetMapping("query")
    public ResultData queryList(HttpServletRequest request){
        String caseId = (String) request.getAttribute("caseId");
        log.info("实际获取到的caseId={}", caseId);
        if(caseId == null){
            return ResultData.error("请求数据异常");
        }
        return ResultData.success(CASE_INFO_LIST.stream().filter(item ->  caseId.equals(item.getId().toString())).findFirst().orElse(null));
    }

    /**
     * 模拟列表查询
     */
    @GetMapping("list")
    public ResultData queryList(){
        // 假设当前登录用户Id为1
        int userId = 1;
        // 业务字段配置 caseId
        // 生成规则可以是: userId,id,"caseId"， 这样不同的接口，相同的用户和id加密结果不一样
        CASE_INFO_LIST.forEach(item -> {
            item.setEncryptId(AesUtil.encrypt(userId + "," + item.getId() + ",caseId"));
        });
        return ResultData.success(CASE_INFO_LIST);
    }
}
